package com.selectionSort;

import com.Sort;

/**
 * @author 罗俊华
 * @date 2021/5/10 - 10:44 上午
 * 选择排序
 */
public class SelectionSort<Item extends Comparable<Item>> extends Sort<Item> {


    public void sort(Item[] comparableArray) {


        // sortedArrayIndex 已排序的数组的索引下标
        for (int sortedArrayIndex = 0; sortedArrayIndex < comparableArray.length; sortedArrayIndex++) {
//           unSortIndex 未排序数组的索引下标

            int minIndex = sortedArrayIndex;
            for (int unSortIndex = sortedArrayIndex; unSortIndex < comparableArray.length; unSortIndex++) {

                if (super.less(comparableArray[unSortIndex], comparableArray[minIndex])) {
                    minIndex = unSortIndex;
                }
            }


            if (minIndex != sortedArrayIndex) {
                super.exchange(comparableArray, sortedArrayIndex, minIndex);
            }

        }
    }
}
